/* eslint-disable vue/no-duplicate-attributes */
<template>
  <div>
    <van-popup
      v-model="feedbackRod"
      position="top"
      :style="{ height: '30%' }"
      get-container="body"
      @close="closeFn2"
    >
      <van-field
        v-model="message"
        rows="2"
        :autosize="{ maxHeight: 150, minHeight: 150 }"
        label="举报内容"
        type="textarea"
        maxlength="50"
        placeholder="请输入内容"
        show-word-limit
        @keydown.enter="ent"
        autofocus
        v-focus
      />
    </van-popup>
    <!-- 一条文章单元格 -->
    <van-cell>
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{ objList.title }}</span>
          <van-image
            :src="objList.cover.images[0]"
            v-if="objList.cover.type === 1"
            lazy-load
            class="thumb"
          >
            <template v-slot:error>图片走丢了</template>
          </van-image>
        </div>
        <div v-if="objList.cover.type > 1" class="thumb-box">
          <van-image v-for="(imgUrl, index) in objList.cover.images"  lazy-load :key="index" :src="imgUrl"  class="thumb">
            <template v-slot:error>加载失败</template>
          </van-image>
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <div>
            <span>{{ objList.aut_name }}</span>
            <span>{{ objList.comm_count }}评论</span>
            <span>{{ date(objList.pubdate) }}</span>
          </div>
          <!-- 反馈按钮 -->
          <van-icon name="cross" @click.stop="show = true" v-show="isShow" />
        </div>
      </template>
    </van-cell>
    <van-action-sheet
      v-model="show"
      :actions="actions"
      :cancel-text="belowText"
      get-container="body"
      @select="selectFn"
      @cancel="returns"
      @close="closeFn"
    />
  </div>
</template>
<script>
import { timeAgo } from '@/utils/date'
import { firstActions, secondActions } from '@/api/report'
export default {
  props: {
    objList: Object, // 单一条文章的数据
    isShow: {
      type: Boolean,
      default: true
    }
  }, // 引入方法必须在 Vue实例中否则不能直接使用
  data () {
    return {
      show: false, // 判断反馈面板是否显示
      actions: firstActions, // 反馈面板内容
      belowText: '取消', // 取消 返回切换
      feedbackRod: false, // 其他举报框状态
      message: '' // 其他举报框内容
    }
  },
  methods: {
    // 处理时间
    date: timeAgo,
    // 切换反馈面板 点击面板选项触发
    selectFn (action, index) {
      console.log(action, index) // action {name: '反馈垃圾内容'}
      if (action.name === '反馈垃圾内容') {
        this.actions = secondActions // 面板重新赋值
        this.belowText = '返回'
      } else if (action.name === '不感兴趣') {
        this.$emit('article', this.objList.art_id) // 触发父组件自定义事件  子 -> 父
        this.show = false
      } else if (action.name === '其它问题') {
        this.message = ''
        this.feedbackRod = true
        this.show = false
      } else {
        this.$emit('report', this.objList.art_id, this.actions.value)
        this.show = false
      }
    },
    // 点击取消 触发
    returns () {
      if (this.belowText === '返回') {
        // 判断是否是返回还是取消 返回则显示，否则隐藏
        this.actions = firstActions
        this.belowText = '取消'
        this.show = true
      }
    },
    // 关闭面板时触发
    closeFn () {
      // 替换为一级面板内容
      this.actions = firstActions
      this.belowText = '取消'
    },
    // 其他举报 键盘事件
    ent () {
      this.feedbackRod = false
      console.log(this.message)
      this.$emit('rest', this.objList.art_id, this.actions.value, this.message)
    },
    // 其他举报 点击蒙版 触发提交
    closeFn2 () {
      if (this.message.length === 0) {
        // eslint-disable-next-line no-useless-return
        return
      }
      this.$emit('rest', this.objList.art_id, this.actions.value, this.message)
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}
// 修改文章显示的图片样式
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover; // 保持原始尺寸比例，但是部分可能被裁剪
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between; // 先两边贴边，在平分剩余空间
}
</style>
